//
//  ChangePassWordViewController.m
//  NoneCar
//
//  Created by DongSen on 2018/7/16.
//  Copyright © 2018年 董森森. All rights reserved.
//

#import "APIManager+Login.h"
#import "ChangePassWordViewController.h"

@interface ChangePassWordViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topToView;
@property (weak, nonatomic) IBOutlet UITextField *oldPasswordTextfield;
@property (weak, nonatomic) IBOutlet UITextField *newspassTextField;

@property (weak, nonatomic) IBOutlet UITextField *confirmPasswordTextfield;
@property (weak, nonatomic) IBOutlet UIButton *confirmButton;

@end

@implementation ChangePassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.topToView.constant = 12+kNAVIGATIONBARHEIGHT;
    self.oldPasswordTextfield.placeholder = @"原密码";
    self.newspassTextField.placeholder = @"新密码（6-15位字母或者数字）";
    self.confirmPasswordTextfield.placeholder = @"确认新密码";
    self.navigationController.navigationBar.translucent=YES;

    [self.navigationController.navigationBar  setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]]
                                                  forBarPosition:UIBarPositionTopAttached
                                                      barMetrics:UIBarMetricsDefault];
    self.titleLabel.textColor = [UIColor blackColor];
    self.title = @"修改密码";
    self.view.backgroundColor =BackGroundColor;
//    self.confirmButton.backgroundColor = STRCOLOR(@"#FF3771E6");
    self.confirmButton.layer.masksToBounds = YES;
    self.confirmButton.layer.cornerRadius = 4;
    // Do any additional setup after loading the view from its nib.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    self.navigationController.navigationBar.hidden = NO;
    if (self.view.frame.origin.x != 0) {
        self.topToView.constant = 12;
    }
    else{
        self.topToView.constant = 12+kNAVIGATIONBARHEIGHT;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)confirmButtonClicked:(UIButton *)sender{
    if (self.oldPasswordTextfield.text.length<6) {
        [self showMessage:@"密码为6-15位字母或者数字"];
        return;
    }
    if (self.newspassTextField.text.length<6) {
        [self showMessage:@"密码为6-15位字母或者数字"];
        return;
    }
    if (self.confirmPasswordTextfield.text.length<6) {
        [self showMessage:@"密码为6-15位字母或者数字"];
        return;
    }
    if (![self.newspassTextField.text isEqualToString:self.confirmPasswordTextfield.text]) {
        [self showMessage:@"两次输入密码不一致"];
        return;
    }
    WeakSelf(self);
    [self showLoading];
    NSDictionary * dic =@{@"UserId":@([UserData sharedUserData].user.UserId),
                          @"PasswordBefore":self.oldPasswordTextfield.text,
                          @"PasswordNew":self.newspassTextField.text
                          };
    [[APIManager sharedNetManager] PostChangePassWord:dic sucess:^(NSInteger code, NSDictionary *dict) {
        [weakSelf hidloading];
        if ([[dict objectForKey:@"Code"] integerValue]==1){
            [weakSelf pop];
            [weakSelf showMessage:@"密码修改成功"];
        }
        else{
            [weakSelf showMessage:[dict objectForKey:@"Message"]];
        }
    } failure:^(NSInteger code, NSError *error) {
        [weakSelf hidloading];
        [weakSelf showNetError];
    }];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString * result;
    if (string.length>0) {
        result = [NSString stringWithFormat:@"%@%@",textField.text,string];
    }
    else{
        result =[textField.text substringToIndex:textField.text.length-1];
    }
    if (textField.text.length>15&&string.length>0) {
        return NO;
    }
    return YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
